package com.grades.springboot.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.grades.springboot.entity.PageBean;
import com.grades.springboot.entity.Result;
import com.grades.springboot.entity.Teacher;
import com.grades.springboot.mapper.SubjectMapper;
import com.grades.springboot.mapper.TeacherMapper;
import com.grades.springboot.service.AdminService;
import com.grades.springboot.util.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private SubjectMapper subjectMapper;

    @Override
    public Result<?> getTeacherList(Integer pageNum, Integer pageSize) {
        PageBean<Teacher> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Teacher> teachers = teacherMapper.getTeacherList();
        Page<Teacher> p =(Page<Teacher>) teachers;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return Result.success(pb);
    }

    @Override
    public Result<?> getTeacherByClass(Integer classId, Integer pageNum, Integer pageSize) {
        PageBean<Teacher> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Teacher> teachers = teacherMapper.getTeacherByClass(classId);
        Page<Teacher> p = (Page<Teacher>) teachers;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return Result.success(pb);
    }

    @Override
    public Result<?> getTeacherBySubject(String subjectName, Integer pageNum, Integer pageSize) {
        PageBean<Teacher> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Teacher> teachers = teacherMapper.getTeacherBySubject(subjectName);
        Page<Teacher> p = (Page<Teacher>) teachers;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return Result.success(pb);
    }

    @Override
    public Result<?> getTeacherById(Integer id, Integer pageNum, Integer pageSize) {
        Teacher teacher = teacherMapper.getTeacherById(id);
        return Result.success(teacher);
    }

    @Override
    public Result<?> getTeacherByName(String name, Integer pageNum, Integer pageSize) {
        PageBean<Teacher> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Teacher> teachers = teacherMapper.getTeacherByName(name);
        Page<Teacher> p = (Page<Teacher>) teachers;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return Result.success(pb);
    }

    @Override
    public Result<?> addTeacher(Teacher teacher) {
        String emailPattern = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
        String phonePattern = "\\d{3}-\\d{8}|\\d{4}-\\{7,8}";
        String idCardPattern = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$";
        String gender = "[男，女]";
        if(!Pattern.matches(emailPattern,teacher.getEmail())) {
            return Result.error("邮箱非法");
        }
        if (!Pattern.matches(phonePattern,teacher.getPhoneNumber())){
            return Result.error("手机号非法");
        }
        if (!Pattern.matches(idCardPattern,teacher.getIdCard())){
            return Result.error("身份证号非法");
        }
        if (!Pattern.matches(gender,teacher.getGender())){
            return Result.error("性别非法");
        }
        String subjectId = subjectMapper.selectByName(teacher.getSubject());
        teacher.setPassword(Encrypt.toMD5(teacher.getIdCard().substring(teacher
                .getIdCard().length() - 6)));
        int flag = teacherMapper.addTeacher(teacher);
        if (flag != 1){
            return Result.error("插入失败");
        }else {
            return Result.success();
        }

    }
}
